<?php
//this model is using for states....
class States_model extends Model{
	
	function States_model()
	{
		parent::Model();
		$this->load->database();
	}
	//sending states name to display to flexigrid...
	
	public function get_states() 
	{
		$this->CI =& get_instance();
		//Select table name
				
		//Build contents query
//		$this->db->query()
		$this->db->select('* from tbl_states ');
		$this->CI->flexigrid->build_query();
		
		//Get contents
		$return['records'] = $this->db->get();
		
		//Build count query
		$this->db->select('count(state_id) as record_count from tbl_states');
		$this->CI->flexigrid->build_query(FALSE);
		$record_count = $this->db->get();
		$row = $record_count->row();
		
		//Get Record Count
		$return['record_count'] = $row->record_count;
	
		//Return all
		return $return;
	}
	
  function insert_state($str = '')
  {
   
    $check = $this->db->query("select * from tbl_states where state_name like '".$str."'");
    
    if($check->num_rows() == 0)
    {
      $this->db->set('state_name', $str);
      $this->db->insert('tbl_states');  
    }
    else
    {
      return 0;
    }
  }
  
  /*
	//to insert new state...
	function insert_state()
	{
		$check = $this->db->query("select * from tbl_states where state_name like '".$_POST['state_name']."'");
		
		if($check->num_rows() == 0)
		{
			$this->db->set('state_name', $_POST['state_name']);
			$this->db->insert('tbl_states');	
		}
		else
		{
			redirect("states/duplicate_state");
		}
		
	}
   
   */
	
	//to select state name to edit....
	function state_edit($id)
	{
		$result = $this->db->query("select * from tbl_states where state_id =".$id);
		return $result;
	}
	//to save state name after modification....
	function save_edit()
	{
		//print_r($_POST);
		$data = array(
               'State_name' => $_POST['state_name'],
            );

	$this->db->where('state_id', $_POST['id']);
	$this->db->update('tbl_states', $data); 
	}
	//to display state name in cities controller...
	function disp_states()
	{
	$result = $this->db->query("select * from tbl_states order by state_name ASC");
	return $result;
	}
	

}

